package IO;

import java.io.*;

/*
 * 
 * */
public class ReaderWriterCopy {
	/**练习一：实现一个源文件读取后，copy到另一个目标文件
	 * */
	private void copyOne(String source,String dest1){
		try {
			//char
			//BIF_input方案(可以选择字符集)
			BufferedReader reader = new BufferedReader
			(new InputStreamReader(new FileInputStream(source)));
			//PBOF_写方案(可以选择字符集)
			PrintWriter writer = new PrintWriter(new BufferedWriter
			(new OutputStreamWriter(new FileOutputStream(dest1))));
			String line;
			while((line=reader.readLine())!=null){
				writer.println(line);
				writer.flush(); 	//清空
			}
			reader.close();
			writer.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch(IOException eIO){
			eIO.printStackTrace();
		}
	}
	/**练习二：copy到两个文件中
	 * */
	private void copyTwo(String source,String dest1,String dest2){
		//char
		try {
			//BF_input方案(不能选择字符集)
			BufferedReader reader = new BufferedReader(new FileReader(source));
			//PBF_写方案(自动缺省字符集)
			PrintWriter writer1 = new PrintWriter(new BufferedWriter(new FileWriter(dest1)));
			//PF_写方案（使用默认字符集写入目标）
			PrintWriter writer2 = new PrintWriter(new FileOutputStream(dest2));
			String line;
			while((line=reader.readLine())!=null){
				writer1.println(line);
				writer2.println(line);
				writer1.flush();
				writer2.flush();
			}
			writer1.close();
			writer2.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch(IOException eIO){
			eIO.printStackTrace();
		}
	}
	public static void main(String[] args){
		ReaderWriterCopy copy = new ReaderWriterCopy();
		String source = "UTF-16Test1.txt";
		String dest1 = "UTF-16Test2.txt";
		
//		String source = "UTF-8Test1.txt";
//		String dest1 = "UTF-8Test2.txt";
		String dest2 = "dest2.txt";
		copy.copyOne(source,dest1);
//		copy.copyTwo(source,dest1,dest2);
	}
}
